﻿using System;
using System.Windows.Forms;
using MagicWatering.Scheduler.Engine;
using MagicWatering.Scheduler.Interfaces;

namespace TestPanel
{
    public partial class TestPanel : Form
    {
        private WateringManager m_manager;
        private readonly IFeedbackService m_feedbackService;

        public TestPanel()
        {
            InitializeComponent();
            comboBoxControllerType.SelectedIndex = 0;
            m_feedbackService = new FeedbackService(this);
        }

        private void OnButtonStartClick(object sender, EventArgs e)
        {
            if( null == m_manager)
            {
                m_manager = new WateringManager(m_feedbackService, textBoxSettingsPath.Text, comboBoxControllerType.Text);
            }
            m_manager.Start();
        }

        private void OnButtonStopClick(object sender, EventArgs e)
        {
            if( null != m_manager)
            {
                m_manager.Stop();
            }
        }

        private delegate void ObjectDelegate(string message);

        public void AddMessage(string message)
        {
            if (listBoxOutput.InvokeRequired)
            {
                ObjectDelegate method = AddMessage;
                Invoke(method, new[] {message});
            }
            else
            {
                listBoxOutput.Items.Add(message);
            }
        }
    }
}
